package com.WFinstitute.controller;

import java.util.ArrayList;
import java.util.List;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;

import com.WFinstitute.domain.Scholar;
import com.WFinstitute.domain.ScholarArticle;
import com.WFinstitute.service.ScholarArticleService;
import com.WFinstitute.service.ScholarService;
import com.WFinstitute.util.Page;
import com.WFinstitute.util.TrimHtmlUtil;


/**
 * 学者处理(与学者有关的所有信息都在此处理)controller
 * @author 匹夫
 * @date 2016-7-25 下午11:31:23
 * @function
 */

@Controller
@RequestMapping("/scholar")
public class ScholarController {
	@Resource
	private ScholarService  scholarService;
	
	@Resource
	private ScholarArticleService  scholarArticleService;
	
	
	@RequestMapping("/scholar_index")
	public String index(HttpServletResponse response,HttpServletRequest request){
		//得到学者的信息
		int id = Integer.parseInt(request.getParameter("id"));
		Scholar scholar = scholarService.findScholarById(id);
		if(scholar.getDescription()!=null&&!scholar.getDescription().equals("")){
			String description = scholar.getDescription();
			//截取500个字符
			scholar.setDescription(TrimHtmlUtil.delHTMLTag(description));
		}
		request.setAttribute("scholar", scholar);
		List<ScholarArticle> scholarArticleList = scholarArticleService.findScholarArticleByScholarname(scholar.getName());
		List<ScholarArticle> scholarArticleListTemp=new ArrayList<ScholarArticle>();
		for (ScholarArticle scholarArticle : scholarArticleList) {
			if(scholarArticle.getArticlecontent()!=null&&!scholarArticle.getArticlecontent().equals(""))
				scholarArticle.setArticlecontent(TrimHtmlUtil.delHTMLTag(scholarArticle.getArticlecontent()));
			scholarArticleListTemp.add(scholarArticle);
		}
		request.setAttribute("scholarArticleList", scholarArticleListTemp);
		return "/WEB-INF/views/scholar/scholar.jsp";
	}
	
	@RequestMapping("/iframe")
	public String iframe(HttpServletResponse response,HttpServletRequest request){
		request.setAttribute("pageIndex", 1);
		request.setAttribute("pageSize", 10);
		return "/WEB-INF/views/scholar/scholar.jsp";
	}
	
	@RequestMapping("/article")
	public String article(HttpServletResponse response,HttpServletRequest request){
		ScholarArticle scholarArticle = scholarArticleService.findScholarArticleById(Integer.parseInt(request.getParameter("id")));
		Integer readingcount = scholarArticle.getReadingcount();
		scholarArticle.setReadingcount(++readingcount);
		scholarArticleService.updateScholarArticle(scholarArticle);
		request.setAttribute("scholarArticle", scholarArticle);
		return "/WEB-INF/views/scholar/article.jsp";
		
	}
	
	//文辅学者照片列表
	@RequestMapping("/photo")
	public String photo(HttpServletResponse response,HttpServletRequest request){
		List<Scholar> scholarList = scholarService.findScholarList();
		request.setAttribute("scholarList", scholarList);
		return "/WEB-INF/views/scholar/scholar_photo.jsp";
	}
	
	@RequestMapping("/description")
	public String description(HttpServletResponse response,HttpServletRequest request){
		int id = Integer.parseInt(request.getParameter("id"));
		Scholar scholar = scholarService.findScholarById(id);
		String description1=scholar.getDescription();
		if(scholar.getDescription()!=null&&!scholar.getDescription().equals("")){
			String description = scholar.getDescription();
			//截取500个字符
			scholar.setDescription(TrimHtmlUtil.delHTMLTag(description));
		}
		request.setAttribute("scholar", scholar);
		request.setAttribute("description1", description1);
		return "/WEB-INF/views/scholar/scholar_description.jsp";
	}
	
	//文章列表
	@RequestMapping("/articlelist")
	public String articleDetail(HttpServletResponse response,HttpServletRequest request){
		 int pageSize=request.getParameter("pageSize")!=null?Integer.valueOf(request.getParameter("pageSize")):6;
	        //获取页面传来当前页码
	    int pageIndex=request.getParameter("pageIndex")!=null?Integer.valueOf(request.getParameter("pageIndex")):1;
		Page<ScholarArticle> scholarArticleListByReading = scholarArticleService.findScholarArticleListByReading(pageSize, pageIndex, "");
		request.setAttribute("page", scholarArticleListByReading);
		request.setAttribute("list", scholarArticleListByReading.getList());
		return "/WEB-INF/views/scholar/scholar_article_list.jsp";
	}
}
